export class UsageManager {
  private static KEY = 'postcard_usage';
  private static MAX_DAILY_USAGE = 3;

  // 获取今天的日期字符串
  private static getTodayString(): string {
    const date = new Date();
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  }

  // 获取使用记录
  static getUsage(): { date: string; count: number } {
    const data = wx.getStorageSync(this.KEY) || { date: this.getTodayString(), count: 0 };
    
    // 如果不是今天的记录，重置计数
    if (data.date !== this.getTodayString()) {
      data.date = this.getTodayString();
      data.count = 0;
      wx.setStorageSync(this.KEY, data);
    }
    
    return data;
  }

  // 增加使用次数
  static incrementUsage(): boolean {
    const data = this.getUsage();
    
    if (data.count >= this.MAX_DAILY_USAGE) {
      return false;
    }
    
    data.count += 1;
    wx.setStorageSync(this.KEY, data);
    return true;
  }

  // 获取剩余次数
  static getRemainingUsage(): number {
    const data = this.getUsage();
    return Math.max(0, this.MAX_DAILY_USAGE - data.count);
  }
} 